<template>
  <Dialog :title="t('candidate.fields.applyDetail')" v-model="dialogVisible" width="70%" top="5vh">
    <!-- 申请信息 -->
    <CardTitle class="block mb-[10px]" :title="t('awardsAudit.title.applyInfo')" />
    <el-descriptions v-loading="formLoading" :column="1" border label-width="230">
      <el-descriptions-item :label="t('awardsAudit.fields.jobCode')">
        {{ applyInfo.jobCode }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('awardsAudit.fields.studentName')">
        {{ applyInfo.studentName }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('awardsAudit.fields.selfIntroduction')">
        {{ applyInfo.selfIntroduction }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('awardsAudit.fields.attachment')">
        <el-button v-if="applyInfo.attachment && applyInfo.attachment !== '[]'" link type="primary" @click="handlePreview">
          {{ t('fileObjectDownload.buttonName') }}
        </el-button>
        <span v-else>{{ t('fileObjectDownload.noFile') }}</span>
      </el-descriptions-item>
    </el-descriptions>
    <!-- 发表论文情况 -->
    <CardTitle class="block my-10px" :title="t('awardsAudit.title.paperSituation')" />
    <SelectedPaperTable
      :list="applyInfo?.studentPaperUseList ?? []"
      @preview="handleTableAttachmentPreview"
    />
    <!-- 专利情况 -->
    <CardTitle class="block my-10px" :title="t('awardsAudit.title.patentSituation')" />
    <SelectedPatentTable
      :list="applyInfo?.studentPatentUseList ?? []"
      @preview="handleTableAttachmentPreview"
    />

    <!-- 文件预览弹窗 -->
    <FilePreviewDialog ref="filePreviewRef" type="pdf" />
  </Dialog>
</template>

<script setup lang="ts">
import SelectedPatentTable from '@/views/awards/candidate/components/SelectedPatentTable.vue'
import SelectedPaperTable from '@/views/awards/candidate/components/SelectedPaperTable.vue'
import FilePreviewDialog from '@/components/FilePreview/FilePreviewDialog.vue'
import { CandidateApi, CandidateVO } from '@/api/awards/candidate'

defineOptions({ name: 'CandidateApplicationInformation' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false)
const formLoading = ref(false)
const applyInfo = ref<Partial<CandidateVO>>({})

const filePreviewRef = ref<InstanceType<typeof FilePreviewDialog>>()
const handlePreview = () => {
  if (!applyInfo.value.attachment) {
    message.error(t('fileObjectDownload.error'))
    return
  }
  filePreviewRef.value?.open(applyInfo.value.attachment)
}

const handleTableAttachmentPreview = (attachment: string) => {
  console.log(attachment)
  if (!attachment) {
    message.error(t('fileObjectDownload.error'))
    return
  }
  filePreviewRef.value?.open(attachment)
}

// 获取申请详情
const getApplyDetail = async (id: string) => {
  formLoading.value = true
  try {
    applyInfo.value = await CandidateApi.getCandidate(id)
  } finally {
    formLoading.value = false
  }
}

// 打开弹窗
const open = async (id: string) => {
  dialogVisible.value = true
  formLoading.value = true
  try {
    await getApplyDetail(id)
  } finally {
    formLoading.value = false
  }
}

defineExpose({
  open
})
</script>

<style scoped lang="scss"></style>
